﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            //加载
            TreeNode tn = new TreeNode {Text="Haha DBMgr" };
            treeView1.Nodes.Add(tn);
            //加载所有库
            DataTable dt = DB.GetTable("master", "select name from sysdatabases");
            pb1.Maximum = dt.Rows.Count;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                pb1.Value = i;
                pb1.Refresh();
                TreeNode node = new TreeNode
                {
                    Text=dt.Rows[i][0].ToString()
                };
                cbxDb.Items.Add(dt.Rows[i][0].ToString());
                tn.Nodes.Add(node);
                //加载库中的表
                LoadTables(node);
            }
            tn.Expand();
            pb1.Visible = pb2.Visible = false;
        }

        private void LoadTables(TreeNode node)
        {
            string db = node.Text;
            string sql = "select name from sysobjects where xtype='u'";
            //获取库中的表
            DataTable dt = DB.GetTable(db, sql);
            pb2.Maximum = dt.Rows.Count;
            //加载到树上
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                pb2.Value = i;
                pb2.Refresh();
                TreeNode tn = new TreeNode
                {
                    Text=dt.Rows[i][0].ToString()
                };
                node.Nodes.Add(tn);
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                //选中的是库
                cbxDb.Text = e.Node.Text;
            }
            else if (e.Node.Level == 2)
            {
                //选中的是表
                cbxDb.Text = e.Node.Parent.Text;
            }
        }

        private void btnExe_Click(object sender, EventArgs e)
        {
            string sql = txtQuery.Text.Trim();
            string tmp = txtQuery.Text.Trim().ToLower();
            if (sql.Length == 0)
            {
                txtQuery.Focus();
                return;
            }
            
            if (tmp.StartsWith("select"))
            {
                //判断SQL是select语句，调用查询操作
                try
                {
                    DataTable dt = DB.GetTable(cbxDb.Text, sql);
                    dgv.DataSource = dt;
                    dgv.Visible = true;
                    lblInfo.Visible = false;
                }
                catch (Exception ex)
                {
                    //UITools.Msg(ex.Message);
                    dgv.Visible = false;
                    lblInfo.Visible = true;
                    lblInfo.Text = "执行出错！原因:" + ex.Message;

                }
            }
            else
            {
                //非查询操作，调用增删改操作
                dgv.Visible = false;
                lblInfo.Visible = true;
                try
                {
                    int n = DB.ExecuteUpdate(cbxDb.Text, sql);
                    lblInfo.Text = "执行成功，影响"+n+"行!";   
                }
                catch (Exception ex)
                {
                    lblInfo.Text = "执行出错！原因:" + ex.Message;
                }
            }
                

        }
    }
}